{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 五行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>ConsoleTables, 2.6.1</span></li><li><span>YiJingFramework.PrimitiveTypes, 4.0.1</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget:ConsoleTables\"\n",
    "#r \"nuget:YiJingFramework.PrimitiveTypes\"\n",
    "using ConsoleTables;\n",
    "using YiJingFramework.PrimitiveTypes;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类型 `Wuxing` 表示五行木、火、土、金、水。它可以与 `string` 、 `int` 之间进行相互转化，实现了 `IComparable` 、 `IEquatable` 和 `IEqualityOperators` 等接口，可以正确地支持排序和比较等行为，支持使用 `==` 等运算符进行比较。\n",
    "\n",
    "## 获取\n",
    "\n",
    "`Wuxing` 中提供了静态属性以直接获取特定五行："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mu 火 Tu 金 Shui\r\n"
     ]
    }
   ],
   "source": [
    "Console.WriteLine($\"{Wuxing.Mu} {Wuxing.Huo:C} {Wuxing.Tu} {Wuxing.Jin:C} {Wuxing.Shui}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Wuxing 和 string 的相互转换\n",
    "\n",
    "上例已经显示了可以使用 `Wuxing.ToString` 将其转为中文或英文的字符串形式，这边给出一个完整的表格："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "| Wuxing      | G    | C  |\r\n",
      "|-------------|------|----|\r\n",
      "| Wuxing.Mu   | Mu   | 木 |\r\n",
      "| Wuxing.Huo  | Huo  | 火 |\r\n",
      "| Wuxing.Tu   | Tu   | 土 |\r\n",
      "| Wuxing.Jin  | Jin  | 金 |\r\n",
      "| Wuxing.Shui | Shui | 水 |\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "Wuxing[] wuxings = [Wuxing.Mu, Wuxing.Huo, Wuxing.Tu, Wuxing.Jin, Wuxing.Shui];\n",
    "var table = new ConsoleTable(\"Wuxing\", \"G\", \"C\");\n",
    "foreach (var wuxing in wuxings)\n",
    "    table.AddRow($\"Wuxing.{wuxing}\", wuxing.ToString(\"G\"), wuxing.ToString(\"C\"));\n",
    "table.Write(Format.Minimal);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 其他语言不会在这个包中支持，不过自己实现一个方法也是相对方便的，也可以借助 [YiJingFramework.EntityRelations](https://yjfwk.yueyinqiu.top/EntityRelations/) 提供的扩展方法加以支持。\n",
    "\n",
    "若要从字符串转换到五行，可以使用 `Parse` 或 `TryParse` 方法，其表现和 `int`、`double` 等内置类型基本一致，支持上述所有转换结果，不区分大小写，允许前后有空白字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MuHuo"
     ]
    }
   ],
   "source": [
    "Console.Write(Wuxing.Parse(\"木 \"));\n",
    "Console.Write(Wuxing.Parse(\"hUO\"));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Wuxing 和 int 的相互转换\n",
    "\n",
    "在 `Wuxing` 和 `int` 之间可以使用强制类型转换，其具体效果如下表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "| i  | w = (Wuxing)i | (int)w |\r\n",
      "|----|---------------|--------|\r\n",
      "| -2 | *金 Jin       | 3      |\r\n",
      "| -1 | *水 Shui      | 4      |\r\n",
      "| 0  |  木 Mu        | 0      |\r\n",
      "| 1  |  火 Huo       | 1      |\r\n",
      "| 2  |  土 Tu        | 2      |\r\n",
      "| 3  |  金 Jin       | 3      |\r\n",
      "| 4  |  水 Shui      | 4      |\r\n",
      "| 5  | *木 Mu        | 0      |\r\n",
      "| 6  | *火 Huo       | 1      |\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "var numbers = Enumerable.Range(0 - 2, 5 + 4);\n",
    "var table = new ConsoleTable(\"i\", \"w = (Wuxing)i\", \"(int)w\");\n",
    "foreach (var i in numbers)\n",
    "{\n",
    "    var w = (Wuxing)i;\n",
    "    var newI = (int)w;\n",
    "    var star = newI == i ? ' ' : '*';\n",
    "    table.AddRow(i, $\"{star}{w:C} {w}\", newI);\n",
    "}\n",
    "table.Write(Format.Minimal);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这种转换一般情况下是不建议使用的，因为它并没有直接的实际意义。它主要是用于在数学上计算获取生克之类，以及便于通过 `switch` 进行匹配。其提供的运算符 `+` 和 `-` 也是如此。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "python",
   "pygments_lexer": "csharp",
   "version": "3.12.2"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
